package modele;

import org.jdom.Document;
import org.jdom.Element;

import outils.Divers;

public class ModeleRelation extends ModeleXml
{
	private  Element eType;
	private Element eClasse1;
	private Element eClasse2;
	private Element eCard1;
	private Element eCard2;
	private Relations typeRelation;
	private ModeleClasse classe1;
	private ModeleClasse classe2;
	private Cardinalite card1;
	private Cardinalite card2;
	
	public ModeleRelation(Relations r, ModeleClasse source,ModeleClasse dest, Cardinalite cardSource,Cardinalite cardDest)
	{
			
		this.init();
		
		this.eType = new Element("type");
		this.setType(r);
		
		//pour la classe 1
		this.eClasse1 = new Element("classe1");
		this.setClasse1(source);
		this.eCard1 = new Element("card1");
		this.setCard1(cardSource);
		
		
		//pour la classe 2
		this.eClasse2 = new Element("classe2");
		this.setClasse2(dest);
		this.eCard2 = new Element("card2");
		this.setCard2(cardDest);
		
		this.placement();
	}

	public ModeleRelation() {}


	public ModeleRelation(Element relationCourante) 
	{
		this(Divers.stringToRelation(relationCourante.getChild("type").getText()),new ModeleClasse(relationCourante.getChild("classe1").getText()), 
				new ModeleClasse(relationCourante.getChild("classe2").getText()),
				Divers.stringToCard(relationCourante.getChild("card1").getText()),
				Divers.stringToCard(relationCourante.getChild("card2").getText()));
		
			System.out.println("------ card1 "+relationCourante.getChild("card1").getText());
			System.out.println("------ card2 "+relationCourante.getChild("card2").getText());

	}


	private void init()
	{
		super.root = new Element("relation");
		super.doc = new Document(root);
	}
	
	private void placement()
	{
		super.root.addContent(this.eType);
		super.root.addContent(this.eClasse1);
		super.root.addContent(this.eCard1);
		super.root.addContent(this.eClasse2);
		super.root.addContent(this.eCard2);
	}
	public Relations getTypeRelation() {
		return Divers.stringToRelation(this.eType.getText());
	}

	public void setTypeRelation(Relations relation) {
		this.typeRelation = relation;
		this.eType.setText(this.typeRelation.toString());
	}

	public ModeleClasse getClasseSource() {
		return classe1;
	}
	
	public ModeleClasse getClasseDest() {
		return classe2;
	}

	public void setClasse1(ModeleClasse src) {
		this.classe1 = src;
		this.eClasse1.setText(this.classe1.getNom());
	}

	public void setClasse2(ModeleClasse dest){
		this.classe2 = dest;
		this.eClasse2.setText(this.classe2.getNom());
	}
	
	public Cardinalite getCardSrc() {
		return card1;
	}
	
	public Cardinalite getCardDest() {
		return card2;
	}

	public void setCard1(Cardinalite card){
		this.card1 = card;
		if(card!=null) this.eCard1.setText(Divers.cardToString(this.card1));
	}
	
	public void setCard2(Cardinalite card){
		this.card2 = card;
		if(card!=null) this.eCard2.setText(Divers.cardToString(this.card2));
	}
	
	public void setType(Relations r) {
		this.typeRelation = r;
		this.eType.setText(this.typeRelation.toString());
	}
}
